<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        /*:root {
            --color: red;
        }*/
        .div-test {
            width: 100px;
            height: 100px;
            border: 1px solid #666;
            background: var(--color)
        }
    </style>
</head>
<body>
<div class="div-test">

</div>
<button id="btn">
按钮
</button>

<script>
    document.querySelector('#btn').addEventListener('click', () => {
        const style = document.createElement('style');

        style.innerHTML = ':root {--color: red;}'
        document.querySelector('body').appendChild(style)
    })
</script>
</body>
</html>